import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:ball_lion/entity/home/task/task_entity.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/resources/styles.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/widgets/loading/no_data_widget.dart';

import 'package:ball_lion/server/api/king_api.dart';
import 'package:ball_lion/widgets/base/wqs_text.dart';
import 'package:ball_lion/widgets/loading/loading_widget.dart';

class PointHistoryPage extends StatefulWidget {
  const PointHistoryPage({super.key});

  @override
  State<PointHistoryPage> createState() => _PointHistoryPageState();
}

class _PointHistoryPageState extends State<PointHistoryPage> {
  List<PointsHistory>? entity;
  int _page = 1;
  EasyRefreshController controller =
      EasyRefreshController(controlFinishLoad: true);

  int get page => _page;
  set page(int value) {
    setState(() {
      _page = value;
    });
  }

  @override
  void initState() {
    refresh();
    super.initState();
  }

  Future refresh() async {
    page = 1;
    entity = await KingApi.getPointsHistory(page, 15);
    controller.resetFooter();
    setState(() {});
  }

  Future loadMore() async {
    page += 1;
    entity?.addAll(await KingApi.getPointsHistory(page, 15));
    controller.finishLoad(entity!.length < page * 15
        ? IndicatorResult.noMore
        : IndicatorResult.success);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Styles.appBar(
          backgroundColor: Colours.transparent, title: QsText('积分明细')),
      backgroundColor: Colours.scaffoldBg,
      body: EasyRefresh.builder(
          controller: controller,
          footer: Styles.refreshFooter(position: IndicatorPosition.locator),
          onRefresh: refresh,
          onLoad: loadMore,
          childBuilder: (context, physics) => LoadingWidget<bool>(
                isLoading: entity == null,
                data: entity?.length == 0,
                noData: NoDataWidget(needScroll: true, physics: physics),
                child: () => ListView(
                  physics: physics,
                  children: List.generate(
                          entity?.length ?? 0, (index) => cell(entity![index]))
                      .append(const FooterLocator()),
                ),
              )),
    );
  }

  Widget cell(PointsHistory data) {
    return Styles.card(
        padding: const EdgeInsets.all(12),
        margin: const EdgeInsets.fromLTRB(8, 10, 8, 0),
        child: Row(
          children: [
            Image.asset(
              Utils.getImgPath('task_point_${data.sign ?? 1}.png'),
              width: 40,
            ),
            const SizedBox(width: 12),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                QsText(
                  data.message ?? "",
                  fontSize: 15,
                  fontWeight: FontWeight.w500,
                ),
                QsText(
                  data.createTime ?? "",
                  color: Colours.grey99,
                  fontSize: 12,
                )
              ],
            ),
            const Spacer(),
            QsText(
              '${data.sign == 1 ? "+" : "-"}${data.points ?? 0}积分',
              color: data.sign == 1 ? Colours.main : Colours.blue2766D6,
              fontSize: 15,
            )
          ],
        ));
  }
}
